package com.personal.system.service.impl.content.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.personal.common.result.Result;
import com.personal.system.entity.content.Message;
import com.personal.system.mapper.content.MessageMapper;
import com.personal.system.service.content.web.ApiMessageService;
import com.xxl.job.core.util.IpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ApiMessageServiceImpl implements ApiMessageService {

    private final MessageMapper messageMapper;

    @Override
    public Result selectMessageList() {
        // 查询留言列表
        List<Message> messageList = messageMapper.selectList(new LambdaQueryWrapper<Message>()
                .select(Message::getId, Message::getNickname, Message::getAvatar,
                        Message::getContent, Message::getTime));
        return Result.ok(messageList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result insertMessage(Message message) {
        // 获取用户ip
        String ipAddress = IpUtil.getIp();
        String ipSource = "湖南";
        message.setIpAddress(ipAddress);
        message.setIpSource(ipSource);
        message.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        messageMapper.insert(message);
        return Result.ok("留言成功");
    }
}